package com.open.rbac.service.id.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import com.open.rbac.model.entity.id.IdentityRel;


@Transactional(rollbackFor = Exception.class)
public interface IdentityRelRepository extends JpaRepository<IdentityRel, String> {

    @Modifying
    @Query("DELETE FROM identity_rel rel WHERE rel.identityId = ?1")
    void deleteReallyByIdentityId(String identityId);

    @Modifying
    @Query("DELETE FROM identity_rel rel WHERE rel.identityId IN (?1)")
    void deleteReallyByIdentityIds(List<String> identityIds);

    @Modifying
    @Query("DELETE FROM identity_rel rel WHERE rel.relIdentityId = ?1")
    void deleteReallyByRelIdentityId(String relIdentityId);

    @Modifying
    @Query("DELETE FROM identity_rel rel WHERE rel.identityId =?1 AND rel.relIdentityId = ?2")
    void deleteReallyByRelIdentityId(String identityId,String relIdentityId);

    @Query("SELECT DISTINCT t1 FROM identity_rel t1 WHERE t1.identityId IN (?1)")
    List<IdentityRel> findByIdentityIds(List<String> identityIds);

}
